<?php
namespace Personell\Model;
use Think\Model;
class CustInterestModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_cust_interest'; 
	
	
	public function saveCustInterest($data){
		$map['custid'] = $data['custid'];
		$map['interest'] = $data['interest'];
		$result = $this->where($map)->find();
		if(empty($result)){
			$result = $this->add($data);
		}else{
			$result = true;
		}
		
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function searchCustList($data){
		$map = array();
		if(!empty($data['interests']) && is_array($data['interests'])){
			$map['qyx_cust_interest.interest']  = array('in',$data['interests']);
		}
	
		if(!empty($data['area'])){
			$map['qyx_cust.area']  = $data['area'];
		}
	
		if($data['sex'] === "0" || $data['sex'] === "1"){
			$map['qyx_cust.sex']  = $data['sex'];
		}
		
		if(!empty($data['custtype']) && is_array($data['custtype'])){
			$map['qyx_cust.custtype']  = array('in' , $data['custtype']);
		}
	
		$map['qyx_cust.status'] = 1;
	
		$total = $this->distinct(true)->field('qyx_cust_interest.custid')->join('join qyx_cust on qyx_cust_interest.custid=qyx_cust.custid')->where($map)->count();
		$fields = array('qyx_cust_interest.custid','qyx_cust.nickname','qyx_cust.realname','qyx_cust.pinyinname',
				'qyx_cust.sex','qyx_cust.age','qyx_cust.area','qyx_cust.signature'); //,'qyx_cust_interest.interest'
	
		if(empty($data['sortField']))$data['sortField'] = "custid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
	
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->distinct(true)->join('join qyx_cust on qyx_cust_interest.custid=qyx_cust.custid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else
			$result = $this->distinct(true)->join('join qyx_cust on qyx_cust_interest.custid=qyx_cust.custid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
	
		if(empty($result)) $result = array();
		return array('total'=>$total , 'data'=>$result);
	}
}


